#!/bin/sh
#
# Brent Smith <gnome@nextreality.net>
# Version: none yet
#
# This script assumes the user select a collection of
# images that they want to resize.  It will prompt them
# for the desired scale size.
#
# Makes the somewhat crappy assumption that anything 
# after the first '.' in the filename is the extension

# file where we can output any non-fatal errors
UNIXTIME=`date '+%s'`
FILETMP="/tmp/convert.$UNIXTIME"

# check for the convert utility
CONVERT=`which convert`
if [ ! -x "$CONVERT"  ]; then
  zenity --info --title "Missing utility" --text "You do not have the ImageMagick convert utility installed, or it is not in the \$PATH."
  exit 1;
fi

# ask the user for the image size
ANGLE=`zenity --list \
             --title="Select the way to rotate the image" \
             --print-column="2" \
             --column="Description" --column="Angle" \
             "Clockwise"         "90"  \
             "Counter-Clockwise" "270" \
             "Flip Vertical"     "180" `

# check to see if the user cancelled the operation
if [ "$?" -ne "0" ]; then
  zenity --info --title "User cancel" --text "User cancelled the operation"
  exit 1;
fi

if [ -z "$ANGLE" -o "$ANGLE" -lt "90" ]; then
  zenity --info --title "Run-time Error" --text "The rotation angle was not specified correctly."
  exit 1;
fi 

# check to make sure the nautilus variable exists/is not empty
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
  zenity --error --title "Environment not set" --text "Environment variable NAUTILUS_SCRIPT_SELECTED_FILE_PATHS not set or empty"
  exit 1;
fi

# must quote environment variable, so word splitting expansion is not performed
# (linefeeds are kept intact)
# also, nautilus seems to put an extra linefeed at the end of the variable so
# we use sed to delete it
SELECTED_FILES=`echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed -e '$d'` 

FILECOUNT=`echo "$SELECTED_FILES" | wc -l`
CUR=0
PERCENT=0

# we assume the user has selected a bunch of files
( echo "$SELECTED_FILES" | while read IMGFILE 
  do

    if [ -z "$IMGFILE" ]; then
      echo '$IMGFILE is empty, this should not happen.' >> $FILETMP
      continue
    elif [ ! -f "$IMGFILE" ]; then
      echo "$IMGFILE is not a regular file.  Please select only image files." >> $FILETMP
      continue
    fi
    
    BASENAME=`basename "$IMGFILE"`
    DIRNAME=`dirname "$IMGFILE"`
    FILEBASE=`echo $BASENAME | cut -f1 -d.`
    FILEEXT=`echo $BASENAME | cut -f2- -d.`
    echo $PERCENT
    echo "#rotating '$IMGFILE' by ${ANGLE} degrees"
    convert -rotate "$ANGLE" "$IMGFILE" "$DIRNAME/${FILEBASE}_${ANGLE}.$FILEEXT"
    CUR=$[$CUR+1]
    PERCENT=$[(${CUR}*100)/$FILECOUNT]

  done

) | zenity --progress \
           --width=640 \
           --height=100 \
           --title "Converting images..." \
           --text "Starting..." \
           --percentage 0 \
           --auto-close

# check to see if the user cancelled the operation
if [ "$?" -ne "0" ]; then
  zenity --info --title "User cancel" --text "User cancelled the operation"
  exit 1;
fi

# check for the existence of the temp file with non-fatal errors
if [ -f $FILETMP ]; then
   zenity --width=480 --height=360 --title "Non fatal errors" --text-info --filename=$FILETMP
fi

